package com.example.springdemo.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.Arrays;
import java.util.HashSet;

/**
 * Swagger3
 *
 * @author wenguang
 * @date 2020/11/26 11:36
 */
@Configuration
@EnableOpenApi
public class Swagger3 {

//    public static final String[] URLS = {
//        "swagger-resources/configuration/ui",
//        "swagger-resources/configuration/security",
//        "swagger-resources",
//        "v3/api-docs",
//    };

    public static final String[] URLS = {
        "/**/swagger-**/**",
        "/**/v3/api-docs"
    };

    @Value("${api.enable}")
    private boolean enable;

    @Value("${api.title}")
    private String title;

    @Value("${api.version}")
    private String version;

    @Value("${api.description}")
    private String description;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30)
            .pathMapping("/")
            .enable(enable)
            .apiInfo(apiInfo())
            //选择哪些接口作为swagger的doc发布
            .select()
            // 为当前包路径
            .apis(/*RequestHandlerSelectors.basePackage("com.example.springdemo")*/RequestHandlerSelectors.any())
            .paths(PathSelectors.any())
            .build()
            .protocols(new HashSet<>(Arrays.asList("http", "https")));
    }

    // 构建 api文档的详细信息函数,注意这里的注解引用的是哪个
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            // 页面标题
            .title(title)
            // 版本号
            .version(version)
            // 描述
            .description(description)
            .build();
    }
}
